<?php

// TODO: Zend_Loader should be configured to require this file automatically

require_once('../application/forms/LoginForm.php');
class GenesisController extends Zend_Controller_Action {

    protected $_defaults = array(
            'groups'=>array(
                1=>'admin',
                2=>'developer',
                3=>'editor',
                4=>'member',
                5=>'guest'
            )
        );

    protected $_registry;
    protected $_config;
    protected $_acl;
    protected $_controllerName;
    protected $_actionName;

    public function init()
    {
    	$this->_registry = Zend_Registry::getInstance();
    	$this->_config = $this->_registry->application_config;
        /**
         * @var Zend_Acl
         */
        $this->_acl = $this->_registry->acl;
        
        $this->view->messages=$this->_helper->flashMessenger->getMessages();

        $this->_controllerName=$this->getRequest()->getControllerName();
        $this->_actionName=$this->getRequest()->getActionName();

        $auth = Zend_Auth::getInstance();

        $this->view->isLoggedIn=false;
        $this->view->username='guest';
        $this->view->jqueryuiTheme=$this->_config->jqueryui->theme;
        $masterGroupName='';
        if ($auth->hasIdentity()) {
            $this->view->isLoggedIn=true;
            $this->view->username=$auth->getIdentity()->username;

            $masterGroupId=$auth->getIdentity()->user_master_group_id;

            if (!isset($this->_defaults['groups'][$masterGroupId]))
                   $masterGroupName='guest';
            else
                $masterGroupName=$this->_defaults['groups'][$masterGroupId];
        if (!$this->_config->installation->done && !$this->_request->isXmlHttpRequest() && $masterGroupName == 'admin' && $this->_controllerName != 'config') {
           $this->_redirect('/config/first-config');
        }


        } else {
            $masterGroupName='guest';
            $form = new forms_LoginForm();
            $this->view->loginForm=$form;
        }
        if (!$this->_acl->isAllowed($masterGroupName,$this->_controllerName,$this->_actionName))
            throw new Exception('Access denied - not enough privileges');
        
        $this->view->navigation()->setDefaultRole($masterGroupName);

    }

	public function preDispatch()
	{
		if($this->_request->isXmlHttpRequest())
			{
				$this->_helper->layout()->disableLayout();
			}
	}
}